﻿
@{
    ViewBag.Title = "SelfTest";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title</title>
    <script type="text/javascript" src="~/Scripts/jquery-1.10.2.min.js"></script>

</head>
<body>
    <div id="progress"></div>
    <div>
        <iframe id="test-frame" style="width:100%;height:600px;" ></iframe>
    </div>
    <script type="text/javascript">
        $(function () {
            var testSteps = new Array();

            function execStep(timeout) {
                if (!timeout) timeout = 15000;
                if (testSteps.length > 0) {
                    var step = testSteps.shift();
                    var ret = step.toString();
                    ret = ret.substr('function '.length);
                    ret = ret.substr(0, ret.indexOf('('));
                    setTimeout(function () {
                        $("#progress").html(ret);
                        step($('#test-frame').get(0).contentWindow.jQuery);
                    }, timeout);
                }
            };
            var realwindow = $('#test-frame').get(0).contentWindow;

            function actuateLink(link) {
                var allowDefaultAction = true;

                if (link.click) {
                    link.click();
                    return;
                }
                else if (document.createEvent) {
                    var e = document.createEvent('MouseEvents');
                    e.initEvent(
                       'click'     // event type
                       , true      // can bubble?
                       , true      // cancelable?
                    );
                    allowDefaultAction = link.dispatchEvent(e);
                }

                if (allowDefaultAction) {
                    var f = realwindow.document.createElement('form');
                    f.action = link.href;
                    realwindow.document.body.appendChild(f);
                    f.submit();
                }
            }

            var realAlert = realwindow.alert;
            var realConfirm = realwindow.confirm;
            var testPath = "@(ViewBag.TestPath)";

            function setConfirm(func) {
                $('#test-frame').get(0).contentWindow.confirm = func;
            }
            function resetConfirm(func) {
                $('#test-frame').get(0).contentWindow.confirm = realConfirm;
            }

            $('#test-frame')[0].src = "@(Url.Content("~/Account/Login") + "?returnUrl=" + ViewBag.TestPath)";

            function login($$) {
                $$("#CloudAccount").val("@(ViewBag.CloudAccount)");
                $$("#Email").val("@(ViewBag.UserName)");
                $$("#Password").val("@(ViewBag.Password)");
                $$(".btn-default").click();
                execStep(20000);
            }


            function deleteFolder1($$) {
                var ele = $$("a[href$='/UITEST/']");
                if (ele.length > 0) {
                    testSteps.unshift(deleteFolder2);
                    actuateLink(ele[0]);
                    execStep();
                } else {
                    execStep(1);
                }
            }

            function deleteFolder2($$) {
                setConfirm(function () { return true });
                actuateLink($$("#removefolder-command")[0]);
                execStep();
            }

            function showCreateFolder($$) {
                actuateLink($$("#newfolder-command")[0]);
                execStep();
            }

            function createFolder($$) {
                $$("#newFolderName").val("UITEST");
                $$("#create-newFolder").click();
                execStep();
            }

            function copy($$) {
                var target = $$("input[name$='/TEST.txt']");
                $$.ajax(realwindow.CloudStorageLight.StorageController.getODataUrl(target.attr("name"), "Copy"), {
                    type: "POST",
                    data: JSON.stringify({ targetId: $$("#currentPath").val() + "UITEST/" }),
                    contentType: "application/json; charset=utf-8",
                });
                execStep();
            }

            function folderTest($$) {
                actuateLink($$("a[href$='/UITEST/']")[0]);
                execStep();
            }


            function rename1($$) {
                actuateLink($$("input[name$='/TEST.txt']")[0]);
                actuateLink($$("#rename-context-command")[0]);
                execStep();
            }
            function rename2($$) {
                $$("#rename-command").click();
                execStep();
            }

            function deleteFile($$) {
                actuateLink($$("input[name$='/TEST.txt']")[0]);
                setConfirm(function () { return true; })
                actuateLink($$("#remove-context-command")[0]);
                execStep();
            }

            function showSetupFolder($$) {
                actuateLink($$("#folder-setup-command")[0]);
                execStep();
            }

            function setSetupFolder($$) {
                $$("#blobItemDetailOkButton").click();
                execStep();
            }

            function redirectPasswordChange($$) {
                actuateLink($$("#changepassword-command")[0]);
                execStep();
            }

            function setPasswordChange($$) {
                $$("input[type='submit']").click();
                execStep();
            }

            testSteps.push(login);
            testSteps.push(deleteFolder1);
            testSteps.push(showCreateFolder);
            testSteps.push(createFolder);
            testSteps.push(copy);
            testSteps.push(folderTest);
            testSteps.push(rename1);
            testSteps.push(rename2);
            testSteps.push(deleteFile);
            testSteps.push(showSetupFolder);
            testSteps.push(setSetupFolder);
            testSteps.push(redirectPasswordChange);
            testSteps.push(setPasswordChange);
            execStep(20000);



        });


    </script>
</body>
</html>

